/**
 * 
 */
var pageNo = 1;     //当前页
var pageSize = 3;   //每页记录数
var dataJson;       //查询参数
var islogin = 1;
var pageList;
$(function(){
    $(".fixed-bottom a").eq(0).click(function () {
        window.location.href = "/huaqiao/front_end/weChat/patriarch/card_weChat_parent.html";
    });
    $(".fixed-bottom a").eq(1).click(function () {
        window.location.href = "/huaqiao/front_end/weChat/patriarch/apply_weChat_parent.html";
    });
    init();
});

function init(){
	setTableBTN();
	query(1);
	$.ajax({
        url: "/huaqiao/front/parent/getCurStu?" + new Date().getTime(),
        async: false,
        success: function (str) {
            if (check(str)) {
                $("#stu_name").text(str.name);
                $("#class_name").text(str.classname);
            }
        }
    });
    $("#date-out-start").focusout(function () {
    	pageNo = 1;
    	queryData();
    });
    $("#date-out-end").focusout(function () {
    	pageNo = 1;
    	queryData();
    });
    $("#date-in-start").focusout(function () {
    	pageNo = 1;
    	queryData();
    });
    $("#date-in-end").focusout(function () {
    	pageNo = 1;
    	queryData();
    });
    $("#date-unusual-start").focusout(function () {
    	pageNo = 1;
    	queryData();
    });
    $("#date-unusual-end").focusout(function () {
    	pageNo = 1;
    	queryData();
    });
	
}

function setTableBTN() {
	$("#page").unbind();
    $("#page").change(function (i) {
    	pageNo = parseInt($("#page").find("option:selected").text());
    	if(pageNo>Pagination.pageCount||pageNo<1||pageNo==Pagination.pageNo){
    		pageNo=Pagination.pageCount;
        }
        queryData();
    });
    $(".page-nav span").each(function (i) {
        if (i == 1) {//上一页按钮
            this.onclick = function () {
            	pageNo=Pagination.pageNo-1;
            	if(pageNo>Pagination.pageCount||pageNo<1||pageNo==Pagination.pageNo){
                    layer.msg("当前是第一页", {time: 1000});
                }
                queryData();
            };
        } else if (i == 2) {//下一页按钮
            this.onclick = function () {
            	pageNo=Pagination.pageNo+1;
            	if(pageNo>Pagination.pageCount||pageNo<1||pageNo==Pagination.pageNo){
                    layer.msg("当前是最后一页", {time: 1000});
                }
                queryData();
            };
        } else if (i == 0) {
            this.onclick = function () {
            	pageNo=1;
                queryData();
            };
        } else if (i == 3) {
            this.onclick = function () {
            	pageNo=Pagination.pageCount;
                queryData();
            };
        }
    });
}

function query(type){
	pageNo = 1;
	queryData();
}

function queryData() {
    dataJson = {
        pageNo : pageNo,
        pageSize : pageSize,
        type: 0
    };
    getDataOfRecord();
}

/**
 * //带参分页查询函数
 */
function getDataOfRecord() {
    $.ajax({
        async:true,
        type:'POST',
        url:'/huaqiao/front/parent/apply',
        data:dataJson,
        cache:false,
        dataType : 'json',
        contentType:'application/x-www-form-urlencoded',
        beforeSend:function (xhr) { },
        success:function (data,textStatus,xhr) {
        	if(check(data)){
        		setPageCode(data.itemCount,null,'wx');
            	setDataOfList(data.list);
        	}
        },
        error:function (xhr,textStatus,errorInfo) { },
        complete:function (xhr,textStatus) {}
    });
}

function setDataOfList(apply) {
    var str = '';
    for(var i=0;i<apply.length;i++){
        str += "<a href='#'><table class='apply-table-we text-center'><tr>";
        str += "<td class='apply-td1'><p>" +apply[i].className + "</p><p>" + apply[i].stuName + "</p></td>";
        str += "<td class='apply-td2'><p>"+apply[i].startTime+"</p><p>至</p><p>" + apply[i].endTime + "</p></td>";
        str += "<td class='apply-td3'><label>请假原因：</label><p class='apply-reason'>"+apply[i].reasonReq+"</p></td>";
        if(apply[i].spStatus==0){
        	str += "<td class='apply-td4'><p class='state-ing'>审批中</p></td>";
        }
        if(apply[i].spStatus==1){
        	str += "<td class='apply-td4'><p class='state-yes'>审批通过</p></td>";
        }
        if(apply[i].spStatus==2){
        	str += "<td class='apply-td4'><p class='state-no'>审批驳回</p></td>";
        }
        if(apply[i].spStatus==3){
        	str += "<td class='apply-td4'><p class='state-cancel'>撤销</p></td>";
        }
        str += "</tr></table></a>";
    }
    $(".record").html(str);
}

/**
 * 检查微信用户是否登陆
 * @param str
 * @returns {Boolean}
 */
function check(str){
	if (str != null && str != "") {
        if (str.errorcode != undefined) {
            if (str.errorcode == 1) {
                if (islogin == 1) {
                    layer.msg(str.errormessage, {time: 1000});
                    // window.location.href = "/huaqiao/front_end/weChat/login_weChat.html";
                    islogin = 0;
                }
            }
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}

